<?php
// +---------------------------------------------------------------------+
// | OneBase    | [ WE CAN DO IT JUST THINK ]                            |
// +---------------------------------------------------------------------+
// | Licensed   | http://www.apache.org/licenses/LICENSE-2.0 )           |
// +---------------------------------------------------------------------+
// | Author     | Bigotry <3162875@qq.com>                               |
// +---------------------------------------------------------------------+
// | Repository | https://gitee.com/Bigotry/OneBase                      |
// +---------------------------------------------------------------------+

namespace app\admin\logic;

use app\common\model\ForumBbs;
use app\common\model\ForumBbsComment;
use app\common\model\GamblerKing;
use app\common\model\KingCategory;
use app\common\model\UserRankingData;
use think\Db;
use think\paginator\driver\Bootstrap;

/**
 * 排行榜逻辑
 */
class Ranking extends AdminBase
{



    /**
     * 获取充值排行榜列表
     */
    public function getRecharge($data = [], $field = 'a.*', $order = '')
    {

        $type=$data['type'];
        $where='u.status=1 and u.is_inside=0';
        $where1=' type= '.$type;
        $where3=['status'=>1,'is_inside'=>0];
        if (!empty($data['date1'])) {
            if (!empty($data['date2'])) {
                $where1.=' and create_time between '.strtotime($data['date1'] . ' 00:00:00') .' and '.strtotime($data['date2'] . ' 23:59:59');

            } else {
                $where1.=' and create_time between '.strtotime($data['date1'] . ' 00:00:00') .' and '.strtotime($data['date1'] . ' 23:59:59');
            }
        }
        if(!empty($data['search_data'])){
            $where.= " and ( u.username= '".$data['search_data']."' or mobile='".$data['search_data']."' or u.id='".$data['search_data']."')";
            $where3['username|mobile|id']=$data['search_data'];
        }

       // dd($where1);
        $pageSize=10;//每页8条
        $currPage=input('page',1);
        $start=($currPage-1)*$pageSize;
        $sql="SELECT u.cover_id,u.id,u.username,u.mobile,u.level_id,u.user_image ,SUM(d.money) AS total_money  FROM tp_member AS u LEFT  JOIN (SELECT *FROM  tp_user_ranking_data WHERE $where1) AS d ON u.id=d.user_id  where $where GROUP BY u.id   ORDER BY total_money   DESC limit $start ,$pageSize  ";
        $total = Member::where($where3)->count();

        $list=Db::query($sql);

        foreach ($list as $k=>$v){
            $list[$k]['image']=get_picture_url($v['cover_id']);
//            $list[$k]['profit_money']=$v->getrecharge($where);
            $list[$k]['recharge_money']=$v['total_money']+0;
        }
        //原生语句分页
        $pagernator = Bootstrap::make($list,$pageSize,$currPage,$total,false,['path'=>Bootstrap::getCurrentPath(),'query'=>request()->param()]);
        $page = $pagernator->render();
        return ['list'=>$list,'count'=>$total,'page'=>$page];
    }





    /**
     * 排行榜编辑
     */
    public function recharge_rank_edit($data=[],$url)
    {




        switch ($data['type']){
            case 1:
                $url=url('recharge_rank');
                break;
            case 2:
                $url=url('profit_rank');
                break;
            case 3:
                $url=url('entrust_rank');
                break;
            case 4:
                $url=url('withdrawal_rank');
                break;
        }
        $data['create_time']=!empty($data['create_time'])?strtotime($data['create_time']):time();
        $result=Db::name('user_ranking_data')->insert([
           'type'=>$data['type'],
           'user_id'=>$data['id'],
           'remark'=>'后台添加',
            'create_time'=>$data['create_time'],
            'money'=>$data['money'],
            'is_user'=>1
       ]);
        return $result ? [RESULT_SUCCESS, '操作成功', $url] : [RESULT_ERROR, $this->modelMember->getError()];
    }


    /**
     * 获取充值排行榜列表
     */
    public function getKingRecharge($data = [], $field = 'a.*', $order = '')
    {

        $type=$data['type'];
        $where='u.status=1 and u.state=2';
        $where1=' type= '.$type;
        $where3=['u.status'=>1,'u.state'=>2];
        if (!empty($data['date1'])) {
            if (!empty($data['date2'])) {
                $where1.=' and create_time between '.strtotime($data['date1'] . ' 00:00:00') .' and '.strtotime($data['date2'] . ' 23:59:59');

            } else {
                $where1.=' and create_time between '.strtotime($data['date1'] . ' 00:00:00') .' and '.strtotime($data['date1'] . ' 23:59:59');
            }
        }
        if(!empty($data['search_data'])){
            $where.= "  and  ( u.name='".$data['search_data']."' or m.mobile='".$data['search_data']."' or m.username='".$data['search_data']."')";
            $where3['name|m.mobile|m.username']=$data['search_data'];
        }

        // dd($where1);
        $pageSize=10;//每页8条
        $currPage=input('page',1);
        $start=($currPage-1)*$pageSize;
        $sql="SELECT m.cover_id,u.user_id,u.id,u.name,m.username,m.mobile ,SUM(d.money) AS total_money  FROM tp_gambler_king AS u INNER JOIN tp_member m on u.user_id=m.id LEFT  JOIN (SELECT *FROM  tp_user_ranking_data WHERE $where1) AS d ON u.id=d.king_id  where $where GROUP BY u.id   ORDER BY total_money   DESC limit $start ,$pageSize  ";
        $total = GamblerKing::alias('u')->join('__MEMBER__ m' ,'u.user_id = m.id')->where($where3)->count();

        $list=Db::query($sql);
//        dd($list);
        foreach ($list as $k=>$v){
            $list[$k]['image']=get_picture_url($v['cover_id']);
//            $list[$k]['profit_money']=$v->getrecharge($where);
            $list[$k]['recharge_money']=$v['total_money']+0;
        }
        //原生语句分页
        $pagernator = Bootstrap::make($list,$pageSize,$currPage,$total,false,['path'=>Bootstrap::getCurrentPath(),'query'=>request()->param()]);
        $page = $pagernator->render();
        return ['list'=>$list,'count'=>$total,'page'=>$page];
    }



    /**
     * 排行榜编辑
     */
    public function king_rank_edit($data=[])
    {


        $info=$this->modelGamblerKing->getInfo(['id'=>$data['id']]);

        switch ($data['type']){
            case 5:
                $url=url('king_profit_rank');
                break;
            case 6:
                $url=url('king_bonus_rank');
                break;
            case 7:
                $url=url('king_entrust_rank');
                break;
            case 8:
                $url=url('king_bets_rank');
                break;
            case 9:
                $url=url('king_entrust_rank');
                break;
        }
        $data['create_time']=!empty($data['create_time'])?strtotime($data['create_time']):time();
        $result=Db::name('user_ranking_data')->insert([
            'type'=>$data['type'],
            'user_id'=>$info['user_id'],
            'king_id'=>$data['id'],
            'create_time'=>$data['create_time'],
            'remark'=>'后台添加',
            'money'=>$data['money'],
            'is_user'=>0
        ]);
        return $result ? [RESULT_SUCCESS, '操作成功', $url] : [RESULT_ERROR, $this->modelGamblerKing->getError()];
    }







}
